package com.jiaxu.wifimusic;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;


public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
    public static final int PLAYORPAUSE = 1;
    public static final int NEXT = 0;
    public static final int CHANGE = 2;
    public static final String TAG = "tagg";
    public static MusicService.Mybinder binder;
    private SeekBar seekBar;
    private TextView time1;
    private TextView time2;
    private TextView artist;
    private TextView title;
    private Handler handler;
    private int userChanged = 0;
    private int progress;


    public static List<MusicInfo> playList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        seekBar = (SeekBar)findViewById(R.id.seekbar);
        time1 = (TextView)findViewById(R.id.time1);
        time2 = (TextView)findViewById(R.id.time2);
        artist = (TextView)findViewById(R.id.artist);
        title = (TextView)findViewById(R.id.title);
        playList = new MediaScaner(this).getPlaylists();
        handler = new Handler();
        Intent intent = new Intent("mymusicservice");
        startService(intent);
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {

                binder = (MusicService.Mybinder) iBinder;
//                binder.setPosition(position);
                seekBar.setMax(binder.getDuration());
                seekBar.setProgress(binder.getProgress());
                time1.setText(FormatTime.formatTime(binder.getProgress()));
                time2.setText(FormatTime.formatTime(binder.getDuration()));
                handler.post(update);
            }
            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        }, BIND_AUTO_CREATE);
        seekBar.setOnSeekBarChangeListener(this);
    }

    Runnable update = new Runnable() {
        @Override
        public void run() {
            switch (userChanged) {
                case 0: //自动读条状态
                    seekBar.setMax(binder.getDuration());
                    seekBar.setProgress(binder.getProgress());
                    time1.setText(FormatTime.formatTime(binder.getProgress()));
                    time2.setText(FormatTime.formatTime(binder.getDuration()));

                    artist.setText(playList.get(binder.getPosition()).getArtist() + "");
                    title.setText(playList.get(binder.getPosition()).getTitle() + "");
                    break;
                case 1:  //按下待命状态
                    time1.setText(FormatTime.formatTime(progress));
                    break;
                case 2:  // 跳转进度并回到自动读条状态
                    binder.setProgress(progress);
                    userChanged = 0;
                    break;
            }
            handler.postDelayed(update, 500); //循环update线程
        }
    };

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser == true) {
            this.progress = progress;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        userChanged = 1;
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        userChanged = 2;
    }

    public void playorpause(View view) {
        Toast.makeText(this,"yes",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent("mymusicservice");
        intent.putExtra("action", PLAYORPAUSE);
        startService(intent);
    }

    public void next(View view) {
        Intent intent = new Intent("mymusicservice");
        intent.putExtra("action", NEXT);
        startService(intent);
    }


    public void list(View view) {
        Intent intent = new Intent(this,PlayList.class);

        startActivity(intent);

    }


}
